package com.stefan.tree;

/*
* tree的接口
* */
public interface StefanTree<E> {
    /*
    * 前中后序遍历方法
    * */
    default void prev(TreeNode<E> root) {
        print(root); //根节点输出
        TreeNode<E> left = root.left;
        if (left != null) {
            prev(left); //左子树作为一个树继续
        }
        TreeNode<E> right = root.right;
        if (right != null) {
            prev(right);//又子树作为一个树继续
        }
    };
    default void mid(TreeNode<E> root) {
        TreeNode<E> left = root.left;
        if (left != null) {
            mid(left); //左子树作为一个树继续
        }
        print(root); //根节点输出

        TreeNode<E> right = root.right;
        if (right != null) {
            mid(right);//又子树作为一个树继续
        }
    };
    default void after(TreeNode<E> root) {
        TreeNode<E> left = root.left;
        if (left != null) {
            after(left); //左子树作为一个树继续
        }
        TreeNode<E> right = root.right;
        if (right != null) {
            after(right);//又子树作为一个树继续
        }
        print(root); //根节点输出
    };

    default void print (TreeNode<E> node) {
        System.out.print(node.getData());
    }
}
